package com.zlt.dao.Impl;

import com.alipay.api.domain.CompanyInfo;
import com.zlt.dao.CompanyDao;
import com.zlt.entity.Company;
import com.zlt.entity.User;
import com.zlt.util.SqlUtil;
import com.zlt.vo.CompanyVO;
import com.zlt.vo.Pager;
import com.zlt.vo.UserVo;

import java.util.List;
import java.util.Map;

public class CompanyDaoImpl implements CompanyDao {

    @Override
    public int insertCompany(Company company) {
        String sql = "insert into company (cname,address,legal_person,contact,phone,lid) values (?,?,?,?,?,?)";

        return SqlUtil.insertReturnId(sql, company.getCname(), company.getAddress(), company.getLegalPerson(), company.getContact(), company.getPhone(), company.getLid());
    }

    public int insertUserCompany(String uid, Integer cid, Integer state) {
        String sql = "insert into user_company (uid,cid,state) values (?,?,?)";
        return SqlUtil.insertReturnId(sql, uid, cid, state);
    }

    @Override
    public Company selectCompanyInfoByUid(String uid, Integer state) {
        String sql = "select company.cid,company.cname,company.address,company.legal_person,company.contact,company.phone,company.state,company.lid from user_company left join company on user_company.cid = company.cid where user_company.uid = ? and user_company.state = ?";
        return SqlUtil.selectOne(Company.class, sql, uid, state);
    }


    public List<Map<String, Object>> selectCompanyByUid(String uid, Integer state) {
        String sql = "select * from user_company where uid=? and state=?";
        return SqlUtil.select(sql, uid, state);
    }

    @Override
    public int insertCompany(String uid, Integer cid, Integer state) {
        String sql = "select company.cid,cname,address,legal_person,contact,phone,company.state,lid from company_comments left join company on company_comments.cid=company.cid where company_comments.uid=?";
        return SqlUtil.update(sql, uid);
    }
    @Override
    public Company selectCompanyInfoByUid(String uid,Integer state1,Integer state2) {
        String sql = "select company.cid,cname,address,legal_person,contact,phone,company.state,lid from user_company left join company on user_company.cid=company.cid where user_company.uid=? and company.state=? and user_company.state=?";
        return SqlUtil.selectOne(Company.class, sql, uid, state1, state2);
    }

    @Override
    public Long selectCountUserHr(String uid) {
        String sql = "select count(*) c from user_company left join user on user_company.uid=user.uid where user_company.uid=?";
        return (Long) SqlUtil.select(sql, uid).get(0).get("c");
    }

    @Override
    public List<User> selectUserHrByCid(Pager<UserVo> pager, Integer cid, Integer state) {
        String sql = "select user.uid,user.username,user.nickname,user.sex,user.email,user_company.state from user left join user_company on user.uid = user_company.uid where user_company.cid=? and user_company.state = ?";
        return SqlUtil.select(User.class, sql, cid, state);
    }

    @Override
    public Long selectReviewedHrCount(String uid) {
        String sql = "select count(*) c from user_company where uid = ? and state = 3";
        return (Long) SqlUtil.select(sql, uid).get(0).get("c");
    }

    @Override
    public List<User> selectUserHr(Pager<UserVo> pager, Integer cid, Integer state) {
        String sql = "select user.uid,user.username,user.nickname,user.sex,user.email,user_company.state from user_company left join user on user_company.uid = user.uid where user_company.cid = ? and user_company.state = ? limit ?,?";
        return SqlUtil.select(User.class, sql, cid, state, (pager.getPage() - 1) * pager.getLimit(), pager.getLimit());
    }

    @Override
    public int updateUserCompanyState(String uid, Integer state) {
        String sql = "update user_company set state = ? where uid =?";
        return SqlUtil.update(sql, state, uid);
    }

    @Override
    public Company selectCompanyByCname(String cname, Integer state) {
        String sql = "select * from company where cname =? and state =?";
        return SqlUtil.selectOne(Company.class, sql, cname, state);
    }

    @Override
    public Company selectCompanyByCid(Integer cid) {
        String sql = "select company.cid,cname,address,legal_person,contact,phone,state,lid from company_comments left join company on company_comments.cid=company.cid where company_comments.cid=?";
        return SqlUtil.selectOne(Company.class, sql, cid);
    }

    public Company selectCompanyInfoByUid(String uid) {
        String sql = "select company.cid,cname,address,legal_person,contact,phone,company.state,lid from user_company left join company on user_company.cid = company.cid where user_company.uid = ?";
        return SqlUtil.selectOne(Company.class, sql, uid);
    }

    @Override
    public Long selectCount(String uid) {
        return null;
    }

    @Override
    public List<Company> selectCompanyInfoByUid(Pager<CompanyVO> pager, String uid) {
        return null;
    }

}
